package com.hongfan.oauth.service.impl;

import com.hongfan.common.dto.AuthUserDto;
import com.hongfan.oauth.service.UserFeignService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zcs
 * @since 2023/10/16
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    UserFeignService userFeignService;

    /**
     * 自定义授权认证
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthUserDto userDto = userFeignService.loadUserByUsername(username);
        String password = passwordEncoder.encode(userDto.getPassword());
        return new User(userDto.getLoginId(),password, AuthorityUtils.commaSeparatedStringToAuthorityList(userDto.getRoles()));
    }
}
